package com.yinhe.module.yzx.gwpx;

import com.yinhe.common.annotation.Log;
import com.yinhe.common.core.controller.BaseController;
import com.yinhe.common.core.domain.AjaxResult;

import com.yinhe.common.core.domain.entity.SysUser;
import com.yinhe.common.core.domain.model.LoginUser;
import com.yinhe.common.core.page.TableDataInfo;
import com.yinhe.common.enums.BusinessType;
import com.yinhe.common.utils.poi.ExcelUtil;

import com.yinhe.system.domain.YhContent;

import com.yinhe.system.msg.mssage.domain.YhMsgInner;
import com.yinhe.system.msg.ws.service.WebSocketService;
import com.yinhe.system.service.IYhContentService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("/yzx/gwpx/gwpx")
public class YzxgwpxController extends BaseController
{
    @Autowired
    private IYhContentService yhContentService;
    @Autowired
    public WebSocketService webSocketService;


    /**
     * 查询内容公告发布列表
     */
    @PreAuthorize("@ss.hasPermi('yzx:gwpx:gwpx:list')")
    @GetMapping("/list")
    public TableDataInfo list(YhContent yhContent)
    {
        startPage();
        List<YhContent> list = yhContentService.selectYhContentList(yhContent);
        return getDataTable(list);
    }

    /**
     * 导出内容公告发布列表
     */
    @PreAuthorize("@ss.hasPermi('yzx:gwpx:gwpx:export')")
    @Log(title = "内容公告发布", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YhContent yhContent)
    {
        List<YhContent> list = yhContentService.selectYhContentList(yhContent);
        ExcelUtil<YhContent> util = new ExcelUtil<YhContent>(YhContent.class);
        util.exportExcel(response, list, "内容公告发布数据");
    }

    /**
     * 获取内容公告发布详细信息
     */
   /* @PreAuthorize("@ss.hasPermi('yzx:gwpx:gwpx:query')")*/
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(yhContentService.selectYhContentById(id));
    }

    /**
     * 新增内容公告发布
     */
    @PreAuthorize("@ss.hasPermi('yzx:gwpx:gwpx:add')")
    @Log(title = "内容公告发布", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody YhContent yhContent)
    {
        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getUser();
        // 存入userId和deptId，做数据权限
        yhContent.setUserId(sysUser.getUserId());
        yhContent.setDeptId(sysUser.getDeptId());
        /*YhMsgInner msgInner = new YhMsgInner();
        msgInner.setMsgTitle("新增一条岗位培训");
        msgInner.setMsgContent(yhContent.getNoticeTitle());
        msgInner.setSendUserName(sysUser.getUserName());
        msgInner.setContentType("");
        String[] split = yhContent.getNote3().split(",");
        List<String> receiveNames = new ArrayList<>();
        for(int i=0;i<split.length;i++)
        {
            receiveNames.add(split[i]);
        }
        webSocketService.PushMsg(msgInner, receiveNames);*/
/*
        YhMsgInner msgInner = new YhMsgInner();
        msgInner.setMsgTitle("新增一条法规制度");
        msgInner.setMsgContent(yhContent.getNoticeTitle());
        msgInner.setSendUserName(sysUser.getUserName());
        msgInner.setContentType("");
        List<String> receiveNames = new ArrayList<>();
        // 这里根据需要添加你要通知的用户名 可以有一个或多个 也就是user表的userName字段
        List<SysUser> result = yhContentService.selectUserByDeptId1 (sysUser.getDeptId());
        for(int i=0; i<result.size(); i++)
        {
           if(result.get(i).getDeptId()==sysUser.getDeptId())
           {
               receiveNames.add(result.get(i).getUserName());
               System.out.println(result.get(i).getUserName());
           }
        }
        receiveNames.add("admin");
        receiveNames.add("juzhang");
        webSocketService.PushMsg(msgInner, receiveNames);*/
        return toAjax(yhContentService.insertYhContent(yhContent));
    }

    /**
     * 修改内容公告发布
     */
    @PreAuthorize("@ss.hasPermi('yzx:gwpx:gwpx:edit')")
    @Log(title = "内容公告发布", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody YhContent yhContent)
    {
        return toAjax(yhContentService.updateYhContent(yhContent));
    }

    /**
     * 删除内容公告发布
     */
    @PreAuthorize("@ss.hasPermi('yzx:gwpx:gwpx:remove')")
    @Log(title = "内容公告发布", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(yhContentService.deleteYhContentByIds(ids));
    }
}
